package com.yantong.controller.api;

import com.yantong.annotation.Auth;
import com.yantong.controller.BaseController;
import com.yantong.pojo.WorkCause;
import com.yantong.pojo.WorkClass;
import com.yantong.pojo.WorkComment;
import com.yantong.pojo.WorkOrder;
import com.yantong.result.ResultVo;
import com.yantong.service.WorkCauseService;
import com.yantong.service.WorkClassService;
import com.yantong.service.WorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author: juzi
 * @date: 2021/10/25
 * @time: 16:12
 */
@ApiIgnore
@Controller
@RequestMapping("/wxapi/workorder")
public class ApiWorkorderController extends BaseController {


    @Autowired
    WorkCauseService workCauseService;

    @Autowired
    WorkClassService workClassService;

    @Autowired
    WorkOrderService workOrderService;



    /**
     * 售后工单分类
     */
    @RequestMapping("/category")
    @ResponseBody
    public ResultVo getWorkOrderCategory() {
        List<WorkClass> categorys = workClassService.listAllTypes();
        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        resultVo.put("data", categorys);
        return resultVo;
    }

    /**
     * 工单类型
     */
    @RequestMapping("/type")
    @ResponseBody
    public ResultVo getWorkOrderType(Long category) {
        List<WorkCause> workCauses = workCauseService.listAllCause(category);
        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        resultVo.put("data", workCauses);
        return resultVo;
    }

    /**
     * 添加售后工单
     */
    @RequestMapping("/add")
    @ResponseBody
    @Auth
    public ResultVo addWorkOrder(WorkOrder workOrder) {
        int result = workOrderService.saveWorkOrder(workOrder);
        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        resultVo.put("data", result);
        return resultVo;
    }

    /**
     * 售后工单详情
     */
    @RequestMapping("/detail")
    @ResponseBody
    @Auth
    public ResultVo getWorkOrder(Long id) {
        WorkOrder workOrder = workOrderService.getByKey(id);
        List<WorkComment> workComment = workOrderService.getWorkComment(id);
        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        resultVo.put("data", workOrder);
        resultVo.put("comment", workComment);
        return resultVo;
    }

    /**
     * 添加售后工单回复
     */
    @RequestMapping("/comment")
    @ResponseBody
    @Auth
    public ResultVo addWorkComment(WorkComment comment) {
        int result = workOrderService.saveWorkComment(comment);
        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        resultVo.put("data", result);
        return resultVo;
    }

    /**
     * 撤销售后工单
     */
    @RequestMapping("/revert")
    @ResponseBody
    @Auth
    public ResultVo revertWorkOrder(Long id) {
        int result = workOrderService.cancelWorkOrder(id);
        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        resultVo.put("data", result);
        return resultVo;
    }
}
